package com.fyzk.utils.jc;

import android.content.Context;

import com.fyzk.activity.lottery.jc.football.JCInfo;
import com.fyzk.model.JCLQMatchData;
import com.fyzk.utils.LogUtil;
import com.fyzk.utils.LotteryIDUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import static java.lang.Double.parseDouble;

public class JLPrizeComputeUtil {

    List<Integer> betcodes;
    List<Boolean> isDanList;
    List<JCLQMatchData> listInfos;
    Context context;
    int lotteryId;

    public JLPrizeComputeUtil(Context context, List<JCLQMatchData> listInfos, int lotteryId) {
        this.context = context;
        initListInfo(listInfos);
        this.listInfos = listInfos;
        this.lotteryId = lotteryId;
    }

    /**
     * 初始化注数必要信息
     */
    public void initListInfo(List<JCLQMatchData> listInfos) {
        this.listInfos = listInfos;
        betcodes = new ArrayList<>();
        isDanList = new ArrayList<>();
        for (int i = 0; i < listInfos.size(); i++) {
            JCLQMatchData jcTeamInfo = listInfos.get(i);
            int checkNum = jcTeamInfo.clickNum;
            if (checkNum > 0) {
                isDanList.add(jcTeamInfo.isDan());
            }
            betcodes.add(checkNum);
        }
    }

    private List<Double> getMaxOddsArraysValue() {
        List<Double> listSelsp = new ArrayList<>();
        for (JCLQMatchData jcInfo : listInfos) {
            int teamNum = jcInfo.clickNum;
            if (teamNum > 0) {
                listSelsp.add(getMaxpl(jcInfo));
            }
        }
        return listSelsp;
    }

    private List<Double> getMinOddsArraysValue() {
        List<Double> listSelsp = new ArrayList<>();
        for (JCLQMatchData jcInfo : listInfos) {
            int teamNum = jcInfo.clickNum;
            if (teamNum > 0) {
                listSelsp.add(getMinSp(jcInfo));
            }
        }
        return listSelsp;
    }

    private Double getMinSp(JCLQMatchData jcInfo){
        ArrayList<Double> btnSps = new ArrayList<>();
        Iterator<String> iterators = jcInfo.splInfos.keySet().iterator();
        while (iterators.hasNext()) {
            String key = iterators.next();
            JCLQMatchData.SPInfo spInfo = jcInfo.splInfos.get(key);
            if (spInfo.isSel) {
                LogUtil.v(jcInfo.getMatchNum()+"|"+spInfo.str+"|"+spInfo.isSel);
                if(spInfo.spValue.contains("-")){
                    btnSps.add(0.00);
                }else{
                    btnSps.add(parseDouble(spInfo.spValue));
                }
            }
        }
        double[] sps = new double[btnSps.size()];
        int ix = 0;
        for (Double d : btnSps) {
            sps[ix] = d;
            ix++;
        }
        Arrays.sort(sps);
        LogUtil.e("sps>>>>>>>>>>>>"+sps[0]);
        return  sps[0];
    }


    private double getMaxpl(JCLQMatchData jcInfo) {
        double maxPl = 0.0d;
        switch (lotteryId) {
            case LotteryIDUtils.JLHH_LOTTERY_ID_INT:
                maxPl = getJLHHMax(jcInfo);
                break;
            case LotteryIDUtils.JLRSF_LOTTERY_ID_INT:
            case LotteryIDUtils.JLSF_LOTTERY_ID_INT:
            case LotteryIDUtils.JLDXF_LOTTERY_ID_INT:
            case LotteryIDUtils.JLSFC_LOTTERY_ID_INT:
                List<JCLQMatchData.SPInfo> iterators = jcInfo.plInfos;
                for (JCLQMatchData.SPInfo spInfo : iterators) {
                    if (spInfo.isSel) {
                        double selpl = 0.00;
                        if(!spInfo.spValue.contains("-")){
                            selpl = Double.parseDouble(spInfo.spValue);
                        }
                        if (selpl > maxPl) {
                            maxPl = selpl;
                        }
                    }
                }
                break;
        }
        return maxPl;
    }

    private double getJLHHMax(JCLQMatchData info) {
        JLHGGroup jlhgGroup = new JLHGGroup(info);
        ArrayList<ArrayList<String>> arrs = jlhgGroup.hgGroup();
        double[] pls = new double[arrs.size()];
        int index = 0;
        for (ArrayList<String> selArr : arrs) {
            for(String key : selArr){
                pls[index] += Double.parseDouble(info.splInfos.get(key).spValue);
            }
            index++;
        }
        Arrays.sort(pls);
        if (pls.length == 0) {
            LogUtil.e("计算混关预计奖金异常……");
            return 1;
        }
        return pls[pls.length - 1];
    }


    public ArrayList<ArrayList<String>> pailieArray(ArrayList<ArrayList<String>> arrss) {
        if (arrss.size() == 1) {
            return arrss;
        } else {
            ArrayList<String> arrss0 = arrss.get(0);
            ArrayList<String> arrss1 = arrss.get(1);
            ArrayList<ArrayList<String>> arrayList0 = new ArrayList<>();

            for (String s0 : arrss0) {
                for (String s1 : arrss1) {
                    ArrayList<String> arr_item = new ArrayList<>();
                    arr_item.add(s0);
                    arr_item.add(s1);
                    arrayList0.add(arr_item);
                }
            }

            if (arrss.size() == 2) {
                return arrayList0;
            } else if(arrss.size() == 3){
                return jisuan(arrayList0, arrss.get(2));
            }else{
                int i = 2;
                ArrayList<ArrayList<String>> arr = new ArrayList<>();
                while (arrss.size() > (i+1)) {
                    arr = jisuan(jisuan(arrayList0, arrss.get(i)), arrss.get(i+1));
                    i++;
                }
                return arr;
            }
        }

    }


    private ArrayList<ArrayList<String>> jisuan(ArrayList<ArrayList<String>> s1s,
                                                ArrayList<String> s2s) {
        ArrayList<ArrayList<String>> ss1 = new ArrayList<>();
        for (ArrayList<String> s1arr : s1s) {
            for (String str2 : s2s) {
                ArrayList<String> arr = new ArrayList<>();
                for (String str1_i : s1arr) {
                    arr.add(str1_i);
                }
                arr.add(str2);
                ss1.add(arr);
            }
        }
        return ss1;
    }

    public double getDanPrize(boolean isMax){
        double prizeValue = 0;
        if(isMax){
            List<Double> prizes = getMaxOddsArraysValue();
            for(Double sp : prizes){
                prizeValue+=sp;
            }
        }else{
            List<Double> prizes = getMinOddsArraysValue();
            double[] prizeSp = new double[prizes.size()];
            int index = 0;
            for(Double sp : prizes){
                prizeSp[index] = sp;
                index++;
            }
            Arrays.sort(prizeSp);
            prizeValue = prizeSp[0];
        }
        return prizeValue*2;
    }


    /**
     * 单关计算注数
     *
     * @return
     */
    public int getDanZhushu() {
        return getDanAAmt(betcodes);
    }

    /**
     * 单关投注计算注数
     *
     * @param betcodes
     * @return
     */
    public static int getDanAAmt(List<Integer> betcodes) {
        int zhushu = 0;
        for (int i = 0; i < betcodes.size(); i++) {
            zhushu += betcodes.get(i);
        }
        return zhushu;
    }

    /**
     * 根据选中的串关名称计算注数
     *
     * @param radioText 过关
     * @return
     */
    public double getDoublePrize(String radioText, boolean isMax) {
        double prize = 0;
        /* 选择2-3场过关投注的，单注最高奖金限额20万米；
         选择4-5场过关投注的，单注最高奖金限额50万米；
         选择6场过关投注的，单注最高奖金限额100万米。
         */
        if (radioText == null) {//单关
            prize = getDanZhushu();
        } else if (radioText.equals("2串1")) {
            prize = getDuoPrize(2, isMax);
        } else if (radioText.equals("3串1")) {
            prize = getDuoPrize(3, isMax);
        } else if (radioText.equals("4串1")) {
            prize = getDuoPrize(4, isMax);
        } else if (radioText.equals("5串1")) {
            prize = getDuoPrize(5, isMax);
        } else if (radioText.equals("6串1")) {
            prize = getDuoPrize(6, isMax);
        } else if (radioText.equals("7串1")) {
            prize = getDuoPrize(7, isMax);
        } else if (radioText.equals("8串1")) {
            prize = getDuoPrize(8, isMax);
        }
        return prize;
    }

    /**
     * 根据选中的串关名称计算注数
     *
     * @param radioText 过关
     * @return
     */
    public double getDoublePrize2(String radioText, List<JCLQMatchData> childInfoData, boolean isMax) {
        double prize = 0;
        /* 选择2-3场过关投注的，单注最高奖金限额20万米；
         选择4-5场过关投注的，单注最高奖金限额50万米；
         选择6场过关投注的，单注最高奖金限额100万米。
         */
        if (radioText == null) {//单关
            prize = getDanZhushu();
        } else if (radioText.equals("2串1")) {
            prize = getDuoPrize2(2, childInfoData, isMax);
        } else if (radioText.equals("3串1")) {
            prize = getDuoPrize2(3, childInfoData, isMax);
        } else if (radioText.equals("4串1")) {
            prize = getDuoPrize2(4, childInfoData, isMax);
        } else if (radioText.equals("5串1")) {
            prize = getDuoPrize2(5, childInfoData, isMax);
        } else if (radioText.equals("6串1")) {
            prize = getDuoPrize2(6, childInfoData, isMax);
        } else if (radioText.equals("7串1")) {
            prize = getDuoPrize2(7, childInfoData, isMax);
        } else if (radioText.equals("8串1")) {
            prize = getDuoPrize2(8, childInfoData, isMax);
        }
        return prize;
    }

    public double getDuoPrize2(int select, List<JCLQMatchData> childInfoData, boolean isMax) {
        initListInfo(childInfoData);
        int isDanNum = getIsDanNum(isDanList);
        if (isMax) {
            isDanNum = 0;
            return JCPrizePermutationandCombination.getFreedomGuoGuanMaxPrize(
                    getMaxOddsArraysValue(), select, isDanList, isDanNum);
        } else {
            return JCPrizePermutationandCombination.getFreedomGuoGuanMixPrize(
                    getMinOddsArraysValue(), select, isDanList, isDanNum);
        }

    }

    /**
     * 获取设胆个数
     *
     * @return
     */
    public int getIsDanNum(List<Boolean> isDanList) {
        int num = 0;
        for (int i = 0; i < isDanList.size(); i++) {
            if (isDanList.get(i)) {
                num++;
            }
        }
        return num;
    }

    public double getDuoPrize(int select, boolean isMax) {
        int isDanNum = getIsDanNum(isDanList);
        if (isMax) {
            return JCPrizePermutationandCombination.getFreedomGuoGuanMaxPrize(
                    getMaxOddsArraysValue(), select, isDanList, isDanNum);
        } else {
            return JCPrizePermutationandCombination.getFreedomGuoGuanMixPrize(
                    getMinOddsArraysValue(), select, isDanList, isDanNum);
        }

    }

    public String getDanGuanPrizeStr(int bei) {
        return JCPrizePermutationandCombination.getNewDanGuanPrize(getMaxOddsArraysValue(), bei);
    }
}
